package com.lxp.uCenterService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxp.uCenterService.domain.UCenterMember;
import com.lxp.uCenterService.service.UCenterMemberService;
import com.lxp.uCenterService.vo.RegisterVo;
import com.lxp.utils.R;
import com.lxp.utils.vo.orderVo.UCenterOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author lxp
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/uCenterService/uCenter_member")
public class UCenterMemberController {
    @Autowired
    private UCenterMemberService service;

    //登录功能
    @PostMapping("/login")
    public R login(@RequestBody UCenterMember member) { //获取登录对象
        String token = service.login(member);
        return R.ok().data("token", token);
    }


    //注册功能
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {
        service.register(registerVo);
        return R.ok();
    }

    //根据token 获取用户
    @GetMapping("/getUserLogin")
    public R getUserLogin(HttpServletRequest request) {
        UCenterMember userLogin = service.getUserLogin(request);
        return R.ok().data("userLogin", userLogin);
    }


    //根据邮箱查询是否被注册
    @GetMapping("/selectByEmail/{email}")
    public R selectByEmail(@PathVariable String email) {
        int count = service.count(new QueryWrapper<UCenterMember>().eq("email", email));
        return R.ok().data("count", count);
    }

    //根据用户名查询用户名是否被注册
    @GetMapping("/selectByName/{name}")
    public R selectByName(@PathVariable String name) {
        int count = service.count(new QueryWrapper<UCenterMember>().eq("nickname", name));
        return R.ok().data("count", count);
    }

    //client根据用户id查询用户信息
    @GetMapping("/getUser/{userId}")
    public UCenterOrderVo getUser(@PathVariable String userId){
        UCenterMember user = service.getById(userId);
        UCenterOrderVo uCenterOrderVo =new UCenterOrderVo();
        BeanUtils.copyProperties(user,uCenterOrderVo);
        return uCenterOrderVo;
    }

}

